package com.kzd.gtboon.base

import android.app.Application
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.*
import com.kzd.gtboon.R
import com.kzd.gtboon.app.SPSConstance
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.kzd.gtboon.manage.getDiskCacheDirectoryApk
import com.module.lemlin.http.*
import com.module.lemlin.app.OwnerApplication
import com.module.lemlin.owner.OwnerViewModel
import com.module.lemlin.owner.setRxLifeScope
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import com.zhihu.matisse.internal.entity.CaptureStrategy
import rxhttp.wrapper.entity.Progress
import java.lang.StringBuilder

open class BaseViewModel(application: Application) : OwnerViewModel(application) {

    override val httpCodeProPer: Int
        get() = 1

    override fun onTokenOverdue() {
//        SPSConstance.clearUser()
//        toAccessActivity()
    }

    val userAppUpdateLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AppUpdate>>>()
    fun userAppUpDate(delay: Long = 0, id: Int) {
        setRxLifeScope(SubscribeViewModel(userAppUpdateLiveData)) {
            appIsUpDate(delay, id.toString())
        }
    }

    companion object {
        val userAppTipsLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AppTips>>>()
    }

    fun userAppTips(delay: Long = 0) {
        setRxLifeScope(SubscribeViewModel(userAppTipsLiveData)) {
            newGetTips(delay)
        }
    }

    val userServiceLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserService>>>()
    fun userService() {
        setRxLifeScope(SubscribeViewModel(userServiceLiveData)) {
            newGetServerInfo()
        }
    }

    val userInfoLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserInfo>>>()
    fun userInfo() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserInfo>>(userInfoLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserInfo>) {
                super.onSuccess(resp)
                SPSConstance.userInfo = resp.data
            }
        }) {
            userGetUserInfo()
        }
    }

    val userMineLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserMine>>>()
    fun userMine() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserMine>>(userMineLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserMine>) {
                super.onSuccess(resp)
                SPSConstance.userMine = resp.data
            }
        }) {
            userMyInfo()
        }
    }


    fun userChange(
        face: String? = null, address: String? = null,
        pet_name: String? = null, gender: String? = null,
        qq: String? = null, name: String? = null, idcard: String? = null,
    ) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserInfo>>(userInfoLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserInfo>) {
                super.onSuccess(resp)
                SPSConstance.userInfo = resp.data
            }
        }) {
            userChangeInfo(
                face = face, address = address, pet_name = pet_name, gender = gender,
                qq = qq, name = name, idcard = idcard
            )
        }
    }

    val userGiftGetLiveData = MutableLiveData<HttpResponseBody<WelfareGiftGetResponse>>()
    fun userGiftGet(lid: String, isDraw: Int, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<WelfareGiftGetResponse>(userGiftGetLiveData) {
            override fun onSuccess(resp: WelfareGiftGetResponse) {
                super.onSuccess(resp.apply {
                    this.isDraw = if (isDraw == 0) 1 else 0
                    this.position = position
                })
            }
        }) {
            appGetGift(lid = lid)
        }
    }

    val userCouponGetLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<WelfareCoupon>>>()

    fun userCouponGet(cid: String) {
        setRxLifeScope(SubscribeViewModel(userCouponGetLiveData)) {
            userAddCoupon(cid = cid)
        }
    }

    val userUploadLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<String>>>()
    fun userUpload(fileName: String? = null, images: MutableList<String>) {
        setRxLifeScope(SubscribeViewModel(userUploadLiveData)) {
            newUploadFile(fileName, images)
        }
    }

    val userDownloadLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun userDownload(
        urlPath: String, progress: (suspend (Progress) -> Unit)? = null,
    ) {
        setRxLifeScope(SubscribeViewModel(userDownloadLiveData)) {
            val destPath =
                StringBuilder(getDiskCacheDirectoryApk(OwnerApplication.application).path)
                    .append("/").append(AppUtils.getAppName()).append(".apk").toString()
            userDownload(urlPath, destPath, progress)
        }
    }

    val userAgreementLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserAgree>>>()
    fun userAgreement(type: String) {
        setRxLifeScope(SubscribeViewModel(userAgreementLiveData)) {
            newGetAgreement(type)
        }
    }

    fun userMatisse(activity: FragmentActivity, maxSelectable: Int = 1) {
        Matisse.from(activity)
            .choose(MimeType.ofImage())
            .capture((true))
            .captureStrategy(CaptureStrategy((true), ("com.kzd.gtboon.fileProvider")))
            .countable((true))
            .maxSelectable(maxSelectable)
            .imageEngine(GlideEngine())
            .theme(R.style.Matisse_Zhihu1)
            .forResult((100))
    }

    val userExchangeCodeListLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<GameExchange>>>()

    fun userExchangeCodeList() {
        setRxLifeScope(SubscribeViewModel(userExchangeCodeListLiveData)) {
            personalGetExchangeCodeList()
        }
    }

}